import { PROTOCAL_SEGMENT_BEGIN, PROTOCAL_SEGMENT_END, PROTOCAL_SEGMENT_SEPARATOR } from './base'

const PROTOCAL_SEGMENT_TABLE = '3'

export function toHtml(params, id, readonly) {
  console.log(params, '88888888888', id, readonly);
  const options = params || {}

  let arr = new Array();
  for (var i = 0; i < params.row; i++) {
    arr[i] = new Array()
    for (var j = 0; j < params.col; j++) {
      arr[i].push('')
    }
  }
  console.log(arr, 'euieueuiueueueueueu');
  let rowOffset = 0
  let rowCount = 1
  let colOffset = 0
  let colCount = 1
  let html = ''
  // const cellContent = '<input type="text" class="record-template-input-text"/>'
  html += '<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"' + 'id="' + id + '"  >'
  if (options.type === 1) {
    rowCount = options.row
    colCount = options.col
    for (let i = rowOffset; i < rowCount; i++) {
      html += '<tr>'
      for (let j = colOffset; j < colCount; j++) {
        let attributes = '<input class="template-col" type="text"'
        if (id) {
          attributes += ' id="' + id + '-' + i + '-' + j + '"'
        }
        if (readonly) {
          attributes += ' disabled'
        }
        if (params.value && params.value.length) {
          attributes += ' value="' + params.value[i][j] + '"'
        }

        html += '<td>' + attributes + 'class="record-template-input-text" style="border-bottom: 0px solid #dcdfe6";text-align: center;  onchange="AbortController(id)" />' + '</td>'
      }
      html += '</tr>'
    }
  } else if (options.type === 2) {
    console.log(options, '999999999');
    if (options.rowTitleList.length > 0) {
      rowOffset++
    }
    if (options.colTitleList.length > 0) {
      colOffset++
    }
    if (rowOffset > 0 && colOffset > 0) {
      rowCount = options.rowTitleList.length + 1
      colCount = options.colTitleList.length + 1
    } else if (rowOffset > 0 && colOffset === 0) {
      rowOffset = 0
      rowCount = options.rowTitleList.length
      colOffset = 1
      colCount = 2
    } else if (rowOffset === 0 && colOffset > 0) {
      colOffset = 0
      colCount = options.colTitleList.length
      rowOffset = 1
      rowCount = 2
    } else {
      return ''
    }
    if (rowOffset > 0) {
      html += '<tr>'
      if (colOffset > 0) {
        html += '<th>&nbsp;</th>'
      }
      let colIndex = 0
      for (let j = colOffset; j < colCount; j++) {
        html += '<th>' + options.colTitleList[colIndex++] + '</th>'
      }
      html += '</tr>'
    }
    let rowIndex = 0
    for (let i = rowOffset; i < rowCount; i++) {
      html += '<tr>'
      if (colOffset > 0) {
        html += '<td>' + options.rowTitleList[rowIndex++] + '</td>'
      }
      for (let j = colOffset; j < colCount; j++) {
        html += '<td>' + '<input type="text" id="' + id + '-' + i + '-' + j + '" class="record-template-input-text" style="border-bottom: 0px solid #dcdfe6;text-align: center;"/>' + '</td>'
      }
      html += '</tr>'
    }
  }
  html += '</table>'
  options.row = rowCount
  options.col = colCount

  return html
}

export function toTemplate(params) {
  const options = params || {}
  console.log(JSON.stringify(options), 'toparams');
  return PROTOCAL_SEGMENT_BEGIN + PROTOCAL_SEGMENT_TABLE + PROTOCAL_SEGMENT_SEPARATOR + JSON.stringify(options) + PROTOCAL_SEGMENT_END
}

/**
 * @description: 提取table-value
 * @param {
 *    html: table dom,
 *    id:dom id
 * }
 * @return {Array}
 */
export function extractTemplateValue(html, id) {
  const data = new Array()
  var rows = html.rows
  for (var i = 0; i < rows.length; i++) {
    data[i] = new Array()
    for (var j = 0; j < rows[i].cells.length; j++) {
      data[i].push(
        document.getElementById(id + '-' + i + '-' + j).value
      )
    }
  }
  return data
}

export function toData(params) {
}

const getColValue = function getTableData(element, id, row, col, rowArray) {
}

const getRowValue = function getTableData(element, id, row, col, tableArray) {
  // getColValue(element, id, row, col, )
}

const geTableValue = function getTableData(element, id, row, col, tableArray) {
  if (element) {
    //
  }
  // getRowValue(element, id, row, col, )
}

export function getTableData(id) {
  const tableArray = []
  const element = document.getElementById(id + '-0-0')
  geTableValue(element, id, 0, 0, tableArray)
  return tableArray
}

export function parseTableToHtml(content, id) {
  if (!content) {
    return null
  }
  if (content.charAt(0) === PROTOCAL_SEGMENT_TABLE) {
    // console.log(content, 'parseTableToHtml')
    const value = content.substring(PROTOCAL_SEGMENT_SEPARATOR.length + 1)
    return toHtml(JSON.parse(value), id)
  }
  return null
}